package cn.dxs.adt;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallbackWithBeforeParam;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.dxs.adt.activity.AppListActivity;
import cn.dxs.adt.activity.BaseActivity;
import cn.dxs.adt.activity.BrvahDemoActivity;
import cn.dxs.adt.activity.CurrentActivity;
import cn.dxs.adt.activity.Heic2jpgActivity;
import cn.dxs.adt.activity.NewsActivity;
import cn.dxs.adt.activity.PosterMakingActivity;
import cn.dxs.adt.adapter.MyAdapter;
import cn.dxs.adt.bean.Icon;
import cn.dxs.adt.http.news.NewsLoader;
import cn.dxs.adt.service.WindowChangeDetectingService;
import demo.MainDemo1Activity;

public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.gv_tools)
    GridView mGv_tools;
    private ArrayList<Icon> mData;
    private MyAdapter<Icon> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initToolBar(toolbar, false, "主界面");
        requestPermission();
        init();
    }

    private void requestPermission() {
        PermissionX.init(this)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .onExplainRequestReason(new ExplainReasonCallbackWithBeforeParam() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList, boolean beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, "即将申请的权限是程序必须依赖的权限", "我已明白");
                    }
                })
                .onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(ForwardScope scope, List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList, "您需要去应用程序设置当中手动开启权限", "我已明白");
                    }
                })
                .request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                        if (allGranted) {
                            Toast.makeText(MainActivity.this, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "您拒绝了如下权限：$deniedList", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void init() {
        initView();
        initData();
    }

    private void initView() {
    }

    private void initData() {
        mData = new ArrayList<Icon>();
        mData.add(new Icon(R.mipmap.iv_icon_1, "APP加固"));
        mData.add(new Icon(R.mipmap.iv_icon_2, "辅助功能"));
        mData.add(new Icon(R.mipmap.iv_icon_3, "分组Demo"));
        mData.add(new Icon(R.mipmap.iv_icon_4, "人民日报"));
        mData.add(new Icon(R.mipmap.iv_icon_5, "heic转jpg"));
        mData.add(new Icon(R.mipmap.iv_icon_6, "海报制作"));
        mData.add(new Icon(R.mipmap.iv_icon_7, "图标7"));
        mData.add(new Icon(R.mipmap.iv_icon_7, "BRVAH Demo"));
        mData.add(new Icon(R.drawable.lostfind, "终端防盗"));
        mData.add(new Icon(R.drawable.call_sms_safe, "通讯卫士"));
        mData.add(new Icon(R.drawable.app_manager, "软件管理"));
        mData.add(new Icon(R.drawable.task_manager, "进程管理"));
        mData.add(new Icon(R.drawable.traffic_statistics, "流量统计"));
        mData.add(new Icon(R.drawable.anti_virus, "病毒查杀"));
        mData.add(new Icon(R.drawable.clean_cache, "缓存清理"));
        mData.add(new Icon(R.drawable.atools, "高级工具"));
        mData.add(new Icon(R.drawable.settin_center, "设置中心"));
        mData.add(new Icon(R.drawable.app_download, "软件下载"));
        mData.add(new Icon(R.drawable.log_manager, "终端日志"));
        mData.add(new Icon(R.drawable.on_off_manager, "开机管理"));

        mAdapter = new MyAdapter<Icon>(mData, R.layout.item_grid_icon) {

            @Override
            public void bindView(ViewHolder holder, Icon obj) {
                holder.setImageResource(R.id.iv_icon, obj.getiId());
                holder.setText(R.id.tv_icon, obj.getiName());
            }
        };

        mGv_tools.setAdapter(mAdapter);
        mGv_tools.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(this, "你点击了~" + position + "~项" + id, Toast.LENGTH_SHORT).show();

        switch (position) {
            case 0:
                startActivity(new Intent(this, AppListActivity.class));
                break;
            case 1:
                startActivity(new Intent(this, CurrentActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, MainDemo1Activity.class));
                break;
            case 3:
                startActivity(new Intent(this, NewsActivity.class));
                break;
            case 4:
                startActivity(new Intent(this, Heic2jpgActivity.class));
                break;
            case 5:
                startActivity(new Intent(this, PosterMakingActivity.class));
                break;
            case 6:
                if (WindowChangeDetectingService.mainFunctions != null) {
                    WindowChangeDetectingService.mainFunctions.handler.sendEmptyMessage(0x00);
                }
                break;
            case 7:
                startActivity(new Intent(this, BrvahDemoActivity.class));
                break;
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 15:
            case 16:
            case 17:
            case 18:
            case 19:
                showToast("暂未开通");
                break;
        }
    }
}